﻿@using KRM.Core

@using KRM.Core.Library
@model KRM.Core.tblOrderItem
@{
    KRMEntities db = ViewBag.db;
    var haspOrder = db.tblParagonOrderItems.Any(a => a.OrderItemID == Model.OrderItemID && a.ParagonVisitID > 0);
    var delivery = db.tblDeliveryReports.FirstOrDefault(a => a.OrderItemID == Model.OrderItemID);
    var order = Model.Order;
    var titleDelivery = db.tblConsignmentNoAndDriverComments.FirstOrDefault(a => a.OrderItemID == Model.OrderItemID);
    var hasPhoto = db.tblOrderItemPhotos.Any(a => a.OrderItemID == Model.OrderItemID);
    var driverPhone = db.Database.SqlQuery<string>(@"SELECT  top 1    LP_Drivers.DriverTelephoneNumber
FROM            tblLoads INNER JOIN
                         tblLoadDrops ON tblLoads.LoadID = tblLoadDrops.LoadID INNER JOIN
                         tblKRMSentStock ON tblLoadDrops.DropID = tblKRMSentStock.DropID INNER JOIN
                         tblKRMOrderItemParts ON tblKRMSentStock.OrderItemPartID = tblKRMOrderItemParts.OrderItemPartID INNER JOIN
                         tblOrderItems ON tblKRMOrderItemParts.OrderItemID = tblOrderItems.OrderItemID INNER JOIN
                         LP_Drivers ON tblLoads.DriverID = LP_Drivers.DriverID 
where tblOrderItems.OrderItemID=" + Model.OrderItemID).FirstOrDefault();
    var orderInRefundRequest = db.tblKRMRefundRequests.Count(a => a.OrderID == order.OrderID) > 0;
    tblUser user = ViewBag.User;
}

<div class="span4" style="padding-top: 17px">
    <div class="span2">
        @if (haspOrder)
        {
            <h1 style="line-height: 10px; color: red">p</h1>
        }
    </div>
    <div class="span10">
        <div class="row-fluid">
            <div class="span6">
                Est Despatch Date:
            </div>
            <div class="span6">
                @if (delivery != null)
                {
                    @String.Format("{0:d MMMM, yyyy}", delivery.DespatchDate);
                }
            </div>
        </div>
        <div class="row-fluid">
            <div class="span6">
                Est Delivery Date:
            </div>
            <div class="span6">
                @if (delivery != null)
                {
                    @String.Format("{0:d MMMM, yyyy}", delivery.DeliveryDate);
                }
            </div>
        </div>
        <div class="row-fluid">
            <div class="span6">
                Drivers Telephone Number:
            </div>
            <div class="span6">
                @driverPhone
            </div>
        </div>
    </div>
</div>
<div class="span2" style="margin-top: 8px">
    <div class="row-fluid"></div>
    <div class="row-fluid">
        @if (titleDelivery != null)
        {
            <img src="~/img/autoship.png" alt="@titleDelivery.LastUpdate @titleDelivery.DriverComments" title="@titleDelivery.LastUpdate @titleDelivery.DriverComments" style="margin: 0 10px" />
        }
        @if (hasPhoto)
        {
            <img src="~/img/camera.png" alt="" onclick="PopupCenter('/orderview/viewphoto?orderitemid=@Model.OrderItemID', 900, 540)" style="cursor: pointer" />
        }
        <img src="~/img/Load/delivery_note_icon.png"alt="" onclick="PopupCenter('/orderview/ManagerDeliveryFiles?orderitemid=@Model.OrderItemID', 1200, 600)" style="cursor: pointer;height: 28px;margin: 0 10px;" />
    </div>
    <div class="row-fluid">
        <a href="javascript:;" onclick="PopupCenter('/orderview/viewpickhistory?orderid=@Model.OrderID',900,650)" class="btn btn-orderitem btn-info">View Pick History</a>
    </div>
</div>
<div class="span6">
    <div class="row-fluid" style="margin-top: 10px">
        <a href="javascript:;" onclick="CheckParagonOrderItem(@Model.OrderID,@Model.OrderItemID)" class="btn span4 btn-info">Change Date</a>
        <a href="javascript:;" onclick="CheckAuditPickingADay(@Model.OrderItemID)" class="btn span4 btn-info">Audit Picking A Day</a>
        <a href="javascript:;" onclick="CheckCancelOrder(@Model.OrderID)" class="btn btn-danger span4">Cancel Order</a>
    </div>
    <div class="row-fluid" style="margin-top: 10px; margin-bottom: 10px">
        <a href="javascript:;" onclick="PopupCenter('/orderview/overridechangedate?orderitemid=@Model.OrderItemID',500,300)" class="btn span4 btn-info">Override Change Date</a>
        <a href="javascript:;" onclick="PopupCenter('/orderview/viewsuboptions?orderitemid=@Model.OrderItemID',900,600)" class="btn span4 btn-info">View Sub Options</a>
        <a href="javascript:;" onclick="CheckCancelOrderItem(@Model.OrderItemID)" class="btn btn-danger span4">Cancel Item</a>
    </div>
    <div class="row-fluid">
        <a href="javascript:;" onclick="@(Users.Current(User.Identity.Name).HasPermission(Permissions.CanChangeVipStatus) ? "PopupCenter('/orderview/assignvipstatus?orderid=" + Model.OrderID + "',500,300)" : "javascript:alert(\"You don't have permission to change vip status!\");")" class="btn span4 btn-info" style="color: red; font-weight: bold">@(order.VIPStatusId > 0 ? db.tblVIPStatuses.Find(order.VIPStatusId).Name : "Assign VIP Status")</a>
        <a class="btn span4 btn-info" onclick="checkorderisinparagon('@order.OrderRef')">Check Order Paragon</a>
        @if (user.HasPermission(Permissions.SendRefundRequest))
        {
            <a href="javascript:;" onclick="GetOrderInRefundRequest()" class="btn btn-info span4">Add Refund Request</a>
        }
    </div>
</div>
<div id="ModalSendRemoveOrderFromLoadEmail" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
</div>
<script>
    function GetOrderInRefundRequest() {
        var orderInRefundRequest = @orderInRefundRequest.ToString().ToLower();
        if (orderInRefundRequest) {
            if(confirm("This Order has confirmed the Refund request. You can only view it")) {
                PopupCenter('/orderview/addrefundrequest?orderid=@order.OrderID', 1100, 670);
            }
        } else {
            PopupCenter('/orderview/addrefundrequest?orderid=@order.OrderID', 1100, 670);
        }
    }
</script>
